package rxjava.application.com.androidmyp.Login;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import rxjava.application.com.androidmyp.BaseActivity;
import rxjava.application.com.androidmyp.Home.HomeActivity;
import rxjava.application.com.androidmyp.Presenter.LoginPresenter;
import rxjava.application.com.androidmyp.R;

public class UserLogin extends BaseActivity implements UserLoginContract {
    @Bind(R.id.userName)
    EditText userName;
    @Bind(R.id.passWord)
    EditText passWord;
    @Bind(R.id.sureToLogin)
    Button sureToLogin;
    private LoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);
        ButterKnife.bind(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        //create presenter
        this.presenter = new LoginPresenter(this);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginPresenter.getClassInfo();
            }
        });
        sureToLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginPresenter.loginWithRetrofit(userName.getText().toString(), passWord.getText().toString());
            }
        });
    }

    @Override
    public void loginSuccess() {
        Toast.makeText(this, "user login success!", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(UserLogin.this, HomeActivity.class));
        this.finish();
    }


    @Override
    public void loginFailed() {
        Toast.makeText(this, "user login Failed!", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setPresenter(Object presenter) {
        this.presenter = loginPresenter = (LoginPresenter) presenter;
    }

}
